package Models;

import byggemarked.Tools;
import java.util.ArrayList;

/**
 * @author Morten
 */
public class CustomerCollection
{
    private ArrayList<Customer> customers;
	private static CustomerCollection instance = new CustomerCollection();

    private CustomerCollection()
    {
        this.customers = new ArrayList<Customer>();
		this.customers.add(new PrivateCustomer("Bent Hansen", "Bakkevej 14", 9800, "Fiskeby", 28718156));
		this.customers.add(new BusinessCustomer("MurenMasterMashers", "Svend Bent", 1715896541, "Rolighedsvej 85", 8974, "Polskiborg", 98178531));
		this.customers.add(new BusinessCustomer("TømmerTammerTrashers", "Hans Hansen", 1748953212, "Fjernholten 1", 7845, "Testby", 47851235));
		this.customers.add(new PrivateCustomer("Hugo Bentsen", "Uglesvinget 902", 3200, "Nørre sønderslev", 98284715));
		this.customers.add(new PrivateCustomer("Poul Jansen", "Klokkevej 89", 8200, "Vodskov", 47875236));
		this.customers.add(new BusinessCustomer("Fresh Fisk Felchers", "Svend Aage", 1824136547, "Testvej 451", 9817, "Hjørring", 98413265));
    }

	public static CustomerCollection getInstance()
	{
		return CustomerCollection.instance;
	}

	public boolean addCustomer(Customer newCustomer)
    {
        return this.customers.add(newCustomer);
    }

    public ArrayList<Customer> findCustomer(String address, int zipCode, String city, int phoneNo, String nameOrCompanyName, String contact, int cvr)
    {
		ArrayList<Customer> foundCustomers = new ArrayList<Customer>();

		for(Customer customer : this.customers)
		{
			if(customer.isActivated())
			{
				boolean found = false;

				if(address != null && !address.equals(""))
				{
					if(customer.getAddress().toLowerCase().contains(address.toLowerCase()))
					{ found = true;	}
				}
				else if(customer.getZipCode() == zipCode)
				{ found = true; }
				else if(city != null && !city.equals(""))
				{
					if(customer.getCity().toLowerCase().contains(city.toLowerCase()))
					{ found = true; }
				}
				else if(customer.getPhoneNo() == phoneNo)
				{ found = true; }
				else if(nameOrCompanyName != null && !nameOrCompanyName.equals(""))
				{
					if(Tools.getNameOrCompanyName(customer).toLowerCase().contains(nameOrCompanyName.toLowerCase()))
					{ found = true; }
				}
				else if(customer instanceof BusinessCustomer)
				{
					BusinessCustomer bCustomer = (BusinessCustomer)customer;
					if(contact != null && !contact.equals(""))
					{
						if(bCustomer.getContact().toLowerCase().contains(contact.toLowerCase()))
						{ found = true; }
					}
					else if(bCustomer.getCvr() == cvr)
					{ found = true; }
				}

				if(found)
				{
					foundCustomers.add(customer);
				}
			}
		}
        return foundCustomers;
    }

	public Customer getCustomer(int id)
    {
		for(Customer customer : this.customers)
		{
			if(customer.getId() == id)
			{
				return customer;
			}
		}
		return null;
    }

	public ArrayList<Customer> getAllCustomers()
	{
		ArrayList<Customer> activatedCustomers = new ArrayList<Customer>();
		for (Customer customer : this.customers)
		{
			if(customer.isActivated())
			{
				activatedCustomers.add(customer);
			}
		}
		return activatedCustomers;
	}

	public void removeCustomer(Customer customer)
    {
        customer.setActivated(false);
    }
}
